package user

import (
	"gitee.com/yangwencheng/vblog/common"
	"golang.org/x/crypto/bcrypt"
)

type CreateUserRequest struct {
	Username string `json:"username" gorm:"column:username"  form:"username" validate:"required"`
	Password string `json:"password" gorm:"column:password" form:"password"  validate:"required"`
}

type UserSet struct {
	*common.PageData
	Items []*User `json:"items"`
}

type User struct {
	*common.Meta
	*CreateUserRequest
}

func (*User) TableName() string {
	return "users"
}

func NewUser() *User {
	return &User{
		Meta:              common.NewMeta(),
		CreateUserRequest: NewCreateUserRequest(),
	}
}

func NewUserSet() *UserSet {
	return &UserSet{
		Items:    []*User{},
		PageData: common.NewPageData(),
	}
}

func NewCreateUserRequest() *CreateUserRequest {
	return &CreateUserRequest{}
}

func NewQueryUserRequest() *QueryUserRequest {
	return &QueryUserRequest{
		PageData: common.NewPageData(),
	}
}

type QueryUserRequest struct {
	*common.PageData
	Username string `json:"username" gorm:"column:username" validate:"required" form:"username"`
	ID       int64  `json:"id" uri:"id"`
}

type DeleteUserRequest struct {
	ID int64 `json:"id" uri:"id" binding:"required" validate:"required"`
}
type ShowUserRequest struct {
	ID int64 `json:"id" uri:"id" binding:"required" validate:"required"`
}

func (req *DeleteUserRequest) Validate() error {
	return common.Validate(req)
}

func (req *ShowUserRequest) Validate() error {
	return common.Validate(req)
}

func (req *CreateUserRequest) Validate() error {
	return common.Validate(req)
}

func (user *User) GeneratePassword() error {

	hp, err := bcrypt.GenerateFromPassword([]byte(user.Password), 10)
	if err != nil {
		return err
	}
	user.Password = string(hp)
	return nil
}

// 验证密码
func (user *User) CheckPassword(pwd string) error {
	err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(pwd))
	if err != nil {
		return err
	}
	return nil
}

type LoginUserRequest struct {
	Username string `json:"username" gorm:"column:username" validate:"required" form:"username"`
	Password string `json:"password" gorm:"column:password" validate:"required" form:"password"`
}

func (req *LoginUserRequest) Validate() error {
	return common.Validate(req)
}

func NewLoginUserRequest() *LoginUserRequest {
	return &LoginUserRequest{}
}

type ValidateTokenRequest struct {
	AccessToken string `json:"accessToken" gorm:"column:access_token" validate:"required" form:"accessToken"`
}

func NewValidateTokenRequest() *ValidateTokenRequest {
	return &ValidateTokenRequest{}
}

func (req *ValidateTokenRequest) Validate() error {
	return common.Validate(req)
}

type UserInfo struct {
	ID               int    `json:"id"`
	Name             string `json:"name"`
	Avatar           string `json:"avatar"`
	Email            string `json:"email"`
	Job              string `json:"job"`
	JobName          string `json:"jobName"`
	Organization     string `json:"organization"`
	OrganizationName string `json:"organizationName"`
	Location         string `json:"location"`
	LocationName     string `json:"locationName"`
	Introduction     string `json:"introduction"`
	PersonalWebsite  string `json:"personalWebsite"`
	Phone            string `json:"phone"`
	RegistrationDate string `json:"registrationDate"`
	AccountId        string `json:"accountId"`
	Certification    string `json:"certification"`
	Role             string `json:"role"`
}

func NewUserInfo() *UserInfo {

	return &UserInfo{
		ID:               0,
		Name:             "",
		Avatar:           "http://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
		Email:            "522350630@qq.com",
		Job:              "goer",
		JobName:          "程序猿",
		Organization:     "backend",
		OrganizationName: "后端",
		Location:         "cq",
		LocationName:     "重庆",
		Introduction:     "无聊的我",
		PersonalWebsite:  "wu",
		Phone:            "184****5668",
		RegistrationDate: "2023-06-07",
		AccountId:        "ad",
		Certification:    "sdfasdf",
		Role:             "admin",
	}
}
